package com.yantong.controller.platform;

import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.InformationGZHTemplate;
import com.yantong.service.InformationGZHTemplateService;
import com.yantong.service.InformationTemplateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


/**
 * 公众号模板消息
 */
@Controller
@RequestMapping("information/GZHtemplate")
public class InformationGZHTemplateController extends BaseController {

    private static final String prefix = "commander/information/GZHtemplate";
    @Autowired
    private InformationTemplateService informationTemplateService;
    @Autowired
    private InformationGZHTemplateService informationGZHTemplateService;

    @RequiresPermissions("commanderList:information:list")
    @GetMapping()
    public String list() {
        return prefix + "/informationList";
    }

    @ResponseBody
    @GetMapping("/list")
    public PageUtils<InformationGZHTemplate> listStorePrinter(String templateName, int offset, int limit) {
        InformationGZHTemplate informationGZHTemplate = new InformationGZHTemplate();
        informationGZHTemplate.setTemplateName(templateName);
        return informationGZHTemplateService.page(informationGZHTemplate, offset, limit);
    }

    @RequiresPermissions("commanderList:information:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }


    @ResponseBody
    @RequiresPermissions("commanderList:information:add")
    @PostMapping("/save")
    public ResultVo save(InformationGZHTemplate informationGZHTemplate ) {
        if (informationGZHTemplateService.insert(informationGZHTemplate) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    @RequiresPermissions("commanderList:information:edit")
    @GetMapping("/edit/{id}")
    public String edit(Model model, @PathVariable Long id) {
        model.addAttribute("information", informationGZHTemplateService.findDetail(id));
        return prefix + "/edit";
    }

    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("commanderList:information:edit")
    public ResultVo update(InformationGZHTemplate informationGZHTemplate) {
        if (informationGZHTemplateService.update(informationGZHTemplate) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    @ResponseBody
    @RequiresPermissions("commanderList:information:remove")
    @PostMapping("/remove")
    public ResultVo remove(Long id) {
        if (id == null) {
            return ResultVo.error();
        }
        InformationGZHTemplate informationGZHTemplate = new InformationGZHTemplate();
        informationGZHTemplate.setId(id);
        informationGZHTemplate.setDeleted(1);
        informationGZHTemplateService.update(informationGZHTemplate);
        return ResultVo.ok();
    }
}
